<template>
	<el-select v-model="currentValue" multiple placeholder="请选择">
		<el-option
				v-for="item in paramsData[parameter]"
				:key="item.id"
				:label="item.name"
				:value="item.id">
		</el-option>
	</el-select>
</template>
<script>
import Vue from 'vue'
import ElementUI from 'element-ui'
import AutoComplete from 'winged/components/wt-autocomplete'

Vue.use(ElementUI);
Vue.component(AutoComplete.name, AutoComplete);

export default {
	name: 'wt-model-field-select',
	props:{
		paramsData:{
			type : Object,
			default : {}
		},
		field:{
			type : Object,
			default : {}
		},
		form:{
			type: Object,
			default : {}
		},
		isId:{
			type: Boolean,
			default: true
		}
	},
	data(){
		return{
			parameter:this.field.optionsParam,
			currentValue:[],
		}
	},
	watch:{
		'currentValue'(val){
			let params = this.paramsData[this.parameter];
			let array = [];
			for(let i in params){
				for (let a in val){
					if (params[i].id == val[a]){
						array.push(params[i])
					}
				}
			}
			if(this.isId){
				this.form[this.field.tableName] = val;
			}else{
				this.form[this.field.tableName] = array;
			}

		}
	},
	created(){
		if (this.form[this.parameter] == ''){
			this.form[this.parameter] == []
		}
		if ((typeof this.form[this.parameter] == 'string'|| typeof this.form[this.parameter] == 'number')&&this.form[this.parameter] !=''){
			let optionsParam = this.form[this.parameter]
			this.currentValue= optionsParam.split(",");
		}else {
			let arr = [];
			for (let i=0;i<this.form[this.parameter].length;i++){
				arr.push(this.form[this.parameter][i].id);
			}
			this.currentValue = arr
		}
	}
}
</script>